package sspersonserver.clientprocessors;
import sspersonserver.Client;
import sspersonserver.ConnectionServer;
import sspersonserver.Server;
import sspersonserver.events.IConListener;
import sspersonserver.events.PacketEvent;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: Double
 * Date: 15.01.12
 * Time: 21:02
 * To change this template use File | Settings | File Templates.
 */
public class MultiClientProcessor implements Runnable,ClientProcessor
{
	private ArrayList<Client> clients = new ArrayList<Client>();
	private boolean flag = true;

	public MultiClientProcessor()
	{
		
	}
	
	public void initListeners()
	{
		ConnectionServer.getInstance().addConListener(new IConListener()
		{
			@Override
			public void onConnect(PacketEvent e)
			{
				System.out.println("Client added");
				addClient((Client) e.getMessage());
			}
		});
		
	}
	
	
	@Override
	public void addClient(Client c)
	{
		clients.add(c);
	}

	@Override
	public void removeClient(Client c)
	{
		clients.remove(c);
	}

	public boolean isValid(Client client)
	{
	 	return true;
	}

	@Override
	public ArrayList<Client> getClients()
	{
		return clients;
	}

	@Override
	public void run()
	{
		System.out.println("MultiClientProcessor run() started");
		while(flag)
		{
			for(Client c:clients)
			{
				if(c.hasMessage())
				{
					System.out.println("blabla");
					Server.getProtocol().decodeSystemPacket(c.getMessage(),c);
				}
			}
			
		}	
	}

}
